package com.njbw.user.config;

import com.njbw.trace.TraceIdContext;

public class TraceIdThread extends Thread {

    private final String traceId;
    private final Runnable delegate;

    public TraceIdThread(ThreadGroup group, Runnable target, String name, long stackSize, String traceId) {
        super(group, target, name, stackSize);
        this.delegate = target;
        this.traceId = traceId;
    }

    @Override
    public void run() {
        TraceIdContext.setTraceId(traceId);
        try {
            delegate.run();
        } finally {
            TraceIdContext.removeTraceId();
        }
    }
}
